package com.rockcent.service.impl;

import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.AuthUserResource;
import com.rockcent.mall.lib.domain.Mall;
import com.rockcent.mall.lib.domain.UserInfo;
import com.rockcent.mall.lib.domain.UserMall;
import com.rockcent.mall.lib.domain.enumclass.SystemType;
import com.rockcent.mall.lib.dto.MenuTreeDTO;
import com.rockcent.mall.lib.dto.UserInfoDto;

import com.rockcent.mall.lib.repository.AuthUserResourceRepository;
import com.rockcent.mall.lib.repository.MallRepository;
import com.rockcent.mall.lib.repository.UserMallRepository;
import com.rockcent.service.MAuthUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by hone on 17-3-13.
 */
@Service
public class MAuthUserServiceImpl extends BaseAuthUserServiceImpl implements MAuthUserService {

    @Autowired
    private AuthUserResourceRepository authUserResourceRepository;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MallRepository mallRepository;
    @Autowired
    private UserMallRepository userMallRepository;

    @Override
    public UserInfoDto login(UserInfo userInfo, HttpServletRequest request) {
        UserInfoDto userInfoDto = null;
        if(userInfo==null || userInfo.getId()==null) {
            return userInfoDto;
        }
        userInfoDto = super.login(userInfo,request);
        return userInfoDto;
    }

    @Override
    public UserInfoDto getManager(Long userId, String domain, String userToken, Long merchantId) {
        UserInfoDto userInfoDto = null;
        if(userId==null || domain==null || userToken==null) {
            return userInfoDto;
        }
        userInfoDto = new UserInfoDto();
        Mall mall = mallRepository.findByHostnameOrCustomerPlatformDomain(domain);
        if(mall==null) {
            List<String> errors = new ArrayList();
            errors.add("domain不存在");
            userInfoDto.setErrorMessage(errors);
            return userInfoDto;
        }
        UserMall userMall = userMallRepository.findByMallIdAndUserId(mall.getId(),userId);
        if (userMall == null) {
            List<String> errors = new ArrayList();
            errors.add("用户没有M端权限");
            userInfoDto.setErrorMessage(errors);
            return userInfoDto;
        }
        if(userMall.getAvailable()==null || userMall.getAvailable() != Whether.YES) {
            List<String> errors = new ArrayList();
            errors.add("用户未启用");
            userInfoDto.setErrorMessage(errors);
            return userInfoDto;
        }
        List<MenuTreeDTO> menuTreeDTOs = null;
        String isSuper=null;
        //验证用户请求是否有权限,除了超级管理员
        if (userMall.getIsSuperAdmin()!=null) {
            if(userMall.getIsSuperAdmin() == Whether.YES) {
                menuTreeDTOs = getAllMenuTrees(SystemType.MALL);
            } else {
                menuTreeDTOs = getMenuTrees(userMall.getUserId(), userMall.getMallId(),userToken);
            }
        }
        isSuper = userMall.getIsSuperAdmin()==null || userMall.getIsSuperAdmin() != Whether.YES?"NO":"YES";

        userInfoDto.setIsSuper(isSuper);
        userInfoDto.setResourceList(menuTreeDTOs);
        userInfoDto.setUserMall(userMall);

        Long userTokenTimer = 30 * 24 * 60 * 60L; // 30天 生命周期
        stringRedisTemplate.boundValueOps(UserInfo.REDIS_MALL_LOGIN_PREFIX + userToken + "_" + domain).set(userMall.getMallId().toString());
        stringRedisTemplate.expire(UserInfo.REDIS_MALL_LOGIN_PREFIX + userToken + "_" + domain, userTokenTimer, TimeUnit.SECONDS);
        return userInfoDto;
    }

    /**
     * 获取对应平台菜单列表
     * @param userId
     * @param mallId
     * @return
     */
    @Override
    public List<MenuTreeDTO> getMenuTrees(Long userId, Long mallId, String userToken) {
        List<AuthUserResource> authUserResources = authUserResourceRepository.findAuthUserResourceByMallIdAndUserId(mallId, userId);
        return getMenuTreeDTOs(authUserResources,userId, SystemType.MALL,true,userToken);
    }

    /**
     * 获取对应平台权限列表
     * @param userId
     * @param mallId
     * @return
     */
    @Override
    public List<AuthUserResource> getResources(Long userId, Long mallId) {
        return authUserResourceRepository.findAuthUserResourceByMallIdAndUserId(mallId, userId);
    }
}
